#!/usr/bin/env python
# coding:utf-8
# @author heitfei
# 2017-06-04 01:14:24

'''
tcp time server using socket
只能维持和处理一个请求，处理完一个请求之后再处理下一个请求
添加 `tsCliSocket.close()` 之后， 处理完立即关闭连接
'''

import socket
import time

HOST = ''
PORT = 23567
BUFFSIZE = 1024
ADDR = (HOST, PORT)

tcpSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
tcpSocket.bind(ADDR)
tcpSocket.listen(5)

while True:
    print "waiting for connection ..."
    tsCliSocket, addr = tcpSocket.accept()
    print "...connected from:",addr

    data = tsCliSocket.recv(BUFFSIZE)
    if not data:
        break
    tsCliSocket.send('[%s] %s' % (time.ctime(), data))
    tsCliSocket.close() # 注释之后，卡在这里等客户端主动关闭，不合理

tcpSocket.close()
